package com.neuedu.oasystem.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.neuedu.oasystem.dao.IBuyPlanDao;
import com.neuedu.oasystem.dao.impl.BuyPlanDaoImpl;
import com.neuedu.oasystem.entity.BuyPlan;
import com.neuedu.oasystem.service.BuyPlanService;
import com.neuedu.oasystem.util.DBHelper;

public class BuyPlanServiceImpl implements BuyPlanService {
	private IBuyPlanDao buyPlanDao;

	public BuyPlanServiceImpl() {
		super();
		this.buyPlanDao = new BuyPlanDaoImpl();
	}

	@Override
	public boolean add(BuyPlan param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			buyPlanDao.setConnection(conn);
			boolean ret = buyPlanDao.add(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}

			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean edit(BuyPlan param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			buyPlanDao.setConnection(conn);
			boolean ret = buyPlanDao.edit(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			buyPlanDao.setConnection(conn);
			boolean ret = buyPlanDao.deleteById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public BuyPlan findById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			buyPlanDao.setConnection(conn);
			BuyPlan assets = buyPlanDao.findById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return assets;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}

			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<BuyPlan> findAll() {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			List<BuyPlan> list = buyPlanDao.findAll();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}

		return null;
	}

	@Override
	public List<BuyPlan> findByExample(BuyPlan param) {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			List<BuyPlan> list = buyPlanDao.findByExample(param);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<BuyPlan> findByPager(int page, int size) {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			List<BuyPlan> list = buyPlanDao.findByPager(page, size);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			int id = buyPlanDao.deleteByIds(ids);
			return id;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int addAll(List<BuyPlan> param) {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			int all = buyPlanDao.addAll(param);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int editAll(List<BuyPlan> param) {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			int all = buyPlanDao.editAll(param);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public List<BuyPlan> findByApproval() {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			List<BuyPlan> list = buyPlanDao.findByApproval();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<BuyPlan> findByRegister() {
		Connection conn = DBHelper.openConnection();
		try {
			buyPlanDao.setConnection(conn);
			List<BuyPlan> list = buyPlanDao.findByRegister();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public boolean editApproval(BuyPlan param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			buyPlanDao.setConnection(conn);
			boolean ret = buyPlanDao.editApproval(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (Exception e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean editRegister(BuyPlan param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			buyPlanDao.setConnection(conn);
			boolean ret = buyPlanDao.editRegister(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

}
